<template>
  <div class="mod-config">
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList()">
      <el-form-item style="float: right">
        <el-button v-if="isAuth('tra:resdiscount:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      row-key="id"
      :header-cell-style="{background:'#F5F7FA',color:'#606266'}"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="resName"
        header-align="center"
        align="center"
        label="餐厅">
      </el-table-column>
      <el-table-column
        prop="eatName"
        header-align="center"
        align="center"
        label="就餐类型">
      </el-table-column>
<!--
      <el-table-column
        prop="discount"
        header-align="center"
        align="center"
        label="折扣比例">
      </el-table-column>
-->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" plain size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" plain size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" :superior="this.superior"/>
  </div>
</template>

<script>
  import AddOrUpdate from './resdiscount-add-or-update'
  import {treeDataTranslate} from '@/utils'

  export default {
    data () {
      return {
        queryForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        superior: []
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/tra/resdiscount/list'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = treeDataTranslate(data.list)
            this.setSuperiors(data.list)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/tra/resdiscount/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      /**
       * 新增修改页面树级数据获取
       * @param resDiscounts
       */
      setSuperiors (resDiscounts) {
        this.superior = []
        for (let resDiscount of resDiscounts) {
          if (resDiscount.type === 0) {
            this.superior.push({label: resDiscount.resName, value: resDiscount.id})
          }
        }
      }
    }
  }
</script>
